package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class OrdersController {
    /**
     * 根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
     * ​           并获取自动生成的订单编号。
     * <p>
     * ​           然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中。
     * <p>
     * ​           然后根据用户编号、商家编号删除购物车表中的数据。
     *
     * @param request
     * @return
     */
    public Object createOrders(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        String daId = request.getParameter("daId");
        String orderTotal = request.getParameter("orderTotal");
        OrdersService ordersService = new OrdersServiceImpl();
        Integer integer = ordersService.createOrders(userId, Integer.valueOf(businessId), Integer.valueOf(daId), Double.valueOf(orderTotal));
        return integer;
    }

    /**
     * 功能：根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     *
     * @param request
     * @return
     */
    public Object getOrdersById(HttpServletRequest request) {
        String orderId = request.getParameter("orderId");
        OrdersService ordersService = new OrdersServiceImpl();
        Orders orders = ordersService.getOrdersById(Integer.valueOf(orderId));
        return orders;
    }

    /**
     * 功能：根据用户编号查询此用户的所有订单信息
     *
     * @param request
     * @return
     */
    public Object listOrdersByUserId(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        OrdersService ordersService = new OrdersServiceImpl();
        List<Orders> list = ordersService.listOrdersByUserId(userId);
        return list;
    }
    public Integer updateState(HttpServletRequest request){
        String orderId = request.getParameter("orderId");
        OrdersService ordersService=new OrdersServiceImpl();
        Integer state = ordersService.updateState(Integer.valueOf(orderId));
        return state;
    }
}
